﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using Eniux.Windows.ThreeDEngine.Helper;
using Eniux.Windows.ThreeDEngine.AttachObject;

namespace Eniux.Windows.ThreeDEngine.Base.CameraControllers
{
    /// <summary>
    ///矩形缩放操作
    /// </summary>
    internal class ZoomRectangleHandler : MouseGestureHandler
    {
        #region 常量、字段

        /// <summary>
        ///   缩放矩形
        /// </summary>
        private Rect zoomRectangle;

        #endregion

        #region 构造、析构

        /// <summary>
        /// 
        /// </summary>
        /// <param name="controller"></param>
        public ZoomRectangleHandler(CameraController controller)
            : base(controller)
        {
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 操作完成时，引发该事件
        /// </summary>
        /// <param name="e"></param>
        public override void Completed(ManipulationEventArgs e)
        {
            base.Completed(e);
            this.Controller.HideRectangle();
            this.ZoomRectangle(this.zoomRectangle);
        }

        /// <summary>
        /// 触摸操作时，位置点发生改变，引发该事件
        /// </summary>
        /// <param name="e"></param>
        public override void Delta(ManipulationEventArgs e)
        {
            base.Delta(e);

            double ar = this.Controller.ActualHeight / this.Controller.ActualWidth;
            var delta = this.MouseDownPoint - e.CurrentPosition;

            if (Math.Abs(delta.Y / delta.X) < ar)
            {
                delta.Y = Math.Sign(delta.Y) * Math.Abs(delta.X * ar);
            }
            else
            {
                delta.X = Math.Sign(delta.X) * Math.Abs(delta.Y / ar);
            }

            this.zoomRectangle = new Rect(this.MouseDownPoint, this.MouseDownPoint - delta);

            this.Controller.UpdateRectangle(this.zoomRectangle);
        }

        /// <summary>
        /// 当操作开始时，引发该事件
        /// </summary>
        /// <param name="e"></param>
        public override void Started(ManipulationEventArgs e)
        {
            base.Started(e);
            this.zoomRectangle = new Rect(this.MouseDownPoint, this.MouseDownPoint);
            this.Controller.ShowRectangle(this.zoomRectangle, Colors.LightGray, Colors.Black);
        }

        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="rectangle"></param>
        public void ZoomRectangle(Rect rectangle)
        {
            if (!AttachCamera.GetIsZoomEnabled(Controller))
            {
                return;
            }

            if (rectangle.Width < 10 || rectangle.Height < 10)
            {
                return;
            }

            CameraHelper.ZoomToRectangle(this.Camera, this.Viewport, rectangle);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override bool CanExecute()
        {
            return AttachCamera.GetIsZoomEnabled(Controller);            
        }

        /// <summary>
        /// 返回缩放光标
        /// </summary>
        /// <returns>光标</returns>
        protected override Cursor GetCursor()
        {
            return AttachCamera.GetZoomRectangleCursor(Controller);
        }

        #endregion
    }
}
